
package com.i3q360.layui.modular.system.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.i3q360.layui.common.base.BaseController;
import com.i3q360.layui.modular.system.entity.SysPermission;
import com.i3q360.layui.modular.system.service.MenuService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController{
		
		@Autowired
		MenuService menuService;
		
		String PREFIX = "/admin/system/";
		
		@RequiresPermissions("menu:view")
		@RequestMapping("")
		public String index(){
			return PREFIX+"menu.html";
		}
		@RequestMapping("/menu-add")
		@RequiresPermissions("menu:add")
		public String add(){
			return PREFIX+"menu-info.html";
		}
		
		@RequiresPermissions("menu:edit")
		@RequestMapping("/menu-edit")
		public String edit(@RequestParam(required=true) Integer id,Model model){
			SysPermission menu = null;
			SysPermission parent = null;
			menu = menuService.queryById(id);
			model.addAttribute("menu", menu);
			if(menu.getParentId()!=0){
				parent = menuService.queryById(menu.getParentId());
				model.addAttribute("parent", parent);
			}
			return PREFIX+"menu-info.html";
		}
		
		
		@ResponseBody
		@RequestMapping("/page")
		public List<SysPermission> page(@RequestParam Map<String,Object> map ){
			
			return menuService.queryList(map);
		}
		
		/**
		 * 获取等级为1的菜单
		 */
		@ResponseBody
		@RequestMapping("/topMenus")
		public List<SysPermission> topMenus(){
			
			List<SysPermission> list =  menuService.queryList("levels",1);
			List<SysPermission> son1 = null;
			List<SysPermission> son2 = null;
			for(SysPermission sys : list){
				son1 = menuService.queryList("pcode", sys.getCode());  //2ji
				if(!son1.isEmpty()){
					for(SysPermission sys2 : son1){
						son2 = menuService.queryList("pcode", sys2.getCode());  //3 ji
						if(!son1.isEmpty()){
							sys2.setChildren(son2);
						}
					}
					sys.setChildren(son1);
				}
			}
			return list;
		}
		/**
		 * 状态变更
		 */
		@ResponseBody
		@RequestMapping("/available")
		public boolean available(boolean available,Integer id){
			SysPermission menu = menuService.queryById(id);
			menu.setAvailable(available);
			menuService.save(menu);
			return true;
		}
		
		/**
		 * 添加菜单
		 */
		@ResponseBody
		@RequestMapping("/save")
		public boolean  add(SysPermission menu){

			if("system".equals(menu.getCode())){
				menu.setPcode("0");
				menu.setPcodes("[0],");
				menu.setLevels(1);
			}else{
				SysPermission pmenu = menuService.queryById(menu.getParentId());
				if(pmenu!=null){
					menu.setPcode(pmenu.getCode());
					menu.setPcodes(pmenu.getPcodes()+"["+pmenu.getCode()+"],");
					menu.setLevels(pmenu.getLevels()+1);
				}	
			}
			menuService.save(menu);
			return true;
		}
 		
		@ResponseBody
		@RequiresPermissions("menu:del")
		@RequestMapping(value="/delete",method=RequestMethod.POST)
		public boolean delete(SysPermission menu){
			if(menu.getId()==null)
				return false;
			
			menuService.delete(menu);
			return true;
		}
		
}
